package jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class JDBCDemo8 {
    public static void main(String[] args) {
        try (Connection connection = DBUtil.getConnection();){
            String sql = "INSERT INTO userinfo(username,password,nickname,age) VALUES (?,?,?,?);";
            PreparedStatement ps = connection.prepareStatement(sql);
            System.out.println("请输入用户名");
            Scanner scanner = new Scanner(System.in);
            String username = scanner.nextLine();
            System.out.println("请输入密码");
            String password = scanner.nextLine();
            System.out.println("请输入昵称");
            String nickname = scanner.nextLine();
            System.out.println("请输入年龄");
            int age = scanner.nextInt();
            ps.setString(1,username);
            ps.setString(2,password);
            ps.setString(3,nickname);
            ps.setInt(4,age);
            int num = ps.executeUpdate();
            if(num>0){
                System.out.println("插入成功");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
